<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/5/7
 * Time: 15:24
 */
namespace app\home\controller;

use app\home\controller\Base;

class Shop extends Base {

    protected $model;   // 实例化模型
    /**
     * 初始化
     */
    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('Shop');
    }

    /**
     * 获取购卡的套餐列表
     */
    public function index()
    {
        $shop = $this->model->getShopListM();
        $this->assign('shop',$shop['package']);
        $this->assign('param',$shop['param']);
        $this->assign('search',$shop['search']);
        return view();
    }

    /**
     * 获取卡片套餐详情
     */
    public function details()
    {
        $details = $this->model->getPackageDetailsM();
        if(!$details){   // 404
            $this->error();
        }
        $this->assign('details',$details);
        // 获取购物车数量
        $cart = $this->model->getCartListM();
        $this->assign('cart',count($cart['cart']));
        return view();
    }
    
    /**
     * 加入购物车
     */
    public function addToCart()
    {
        $result = $this->model->addToCartM();
        return $result;
    }

    /**
     * 获取购物车详情
     */
    public function cart()
    {
        $cart = $this->model->getCartListM();
        $this->assign('cart',$cart['cart']);
        $this->assign('total_price',$cart['total_price']);
        return view();
    }
    
    /**
     * 删除购物车数据
     */
    public function deleteCart()
    {
        $result = $this->model->deleteCartM();
        return $result;
    }

    /**
     * 购物车进行结算
     */
    public function settlement()
    {
        // 获取购物车列表
        $cart = $this->model->getCartListM();
        $this->assign('cart',$cart['cart']);
        $this->assign('total_price',$cart['total_price']);
        $this->assign('total_number',$cart['total_number']);
        // 获取收货地址
        $address = $this->model->getReceivingAddressM();
        $this->assign('address',$address);
        // 获取快递列表
        $express = $this->model->getExpressListM($address[0]['province']);
        $this->assign('express',$express);
        return view();
    }

    /**
     * 根据地址信息获取快递列表
     */
    public function getExpress()
    {
        $express = $this->model->getExpressListM();
        if(!$express){
            return ['code'=>1,'express'=>$express];
        }
        return ['code'=>0,'express'=>$express];
    }

    /**
     * 提交订单
     */
    public function submitOrder()
    {
        $result = $this->model->submitOrderM();
        return $result;
    }

}